home *** CD-ROM | disk | FTP | other *** search
/ Champak 43 / Vol 43.iso / games / phit.swf / scripts / __Packages / Intersection.as < prev    next >
Encoding:
Text File  |  2007-07-13  |  9.7 KB  |  239 lines

  1. class Intersection
  2. {
  3.    function Intersection()
  4.    {
  5.    }
  6.    static function DoIntersect(volume1, volume2)
  7.    {
  8.       if(!volume1.IsColliding() || !volume2.IsColliding())
  9.       {
  10.          return null;
  11.       }
  12.       if(!volume1.MayCollideOnSeparation() && !volume2.MayCollideOnSeparation())
  13.       {
  14.          if(volume1.m_body && volume2.m_body)
  15.          {
  16.             var _loc5_ = volume1.m_body._effectiveVelocity.GetSubtract(volume2.m_body._effectiveVelocity);
  17.             var _loc6_ = volume1.m_body._location.GetSubtract(volume2.m_body._location);
  18.             if(_loc5_.DotProduct(_loc6_) > 0)
  19.             {
  20.                return null;
  21.             }
  22.          }
  23.       }
  24.       if(volume1 instanceof CHitTestCollision)
  25.       {
  26.          var _loc4_ = volume1.m_body.GetWorldSpaceLocation();
  27.          return !volume2.m_body.hitTest(_loc4_._x,_loc4_._y,false) ? null : new CCollisionInfo();
  28.       }
  29.       if(volume2 instanceof CHitTestCollision)
  30.       {
  31.          var _loc3_ = volume2.m_body.GetWorldSpaceLocation();
  32.          return !volume1.m_body.hitTest(_loc3_._x,_loc3_._y,false) ? null : new CCollisionInfo();
  33.       }
  34.       if(volume1 instanceof CCircle && volume2 instanceof CCircle)
  35.       {
  36.          return Intersection.DoIntersectCircleCircle(CCircle(volume1),CCircle(volume2));
  37.       }
  38.       if(volume1 instanceof CCVAxisAlignedBox && volume2 instanceof CCVAxisAlignedBox)
  39.       {
  40.          return Intersection.DoIntersectAABAAB(CCVAxisAlignedBox(volume1),CCVAxisAlignedBox(volume2));
  41.       }
  42.       if(volume1 instanceof CLineSegment && volume2 instanceof CCircle)
  43.       {
  44.          return Intersection.DoIntersectCircleLineSegment(CCircle(volume2),CLineSegment(volume1));
  45.       }
  46.       if(volume1 instanceof CCircle && volume2 instanceof CLineSegment)
  47.       {
  48.          return Intersection.DoIntersectCircleLineSegment(CCircle(volume1),CLineSegment(volume2));
  49.       }
  50.       if(volume1 instanceof CCircle && volume2 instanceof CCVAxisAlignedBox)
  51.       {
  52.          return Intersection.DoIntersectCircleAAB(CCircle(volume1),CCVAxisAlignedBox(volume2));
  53.       }
  54.       if(volume1 instanceof CCVAxisAlignedBox && volume2 instanceof CCircle)
  55.       {
  56.          return Intersection.DoIntersectCircleAAB(CCircle(volume2),CCVAxisAlignedBox(volume1));
  57.       }
  58.       if(volume1 instanceof CCVAxisAlignedBox && volume2 instanceof CLineSegment)
  59.       {
  60.          return Intersection.DoIntersectAABLineSegment(CCVAxisAlignedBox(volume1),CLineSegment(volume2));
  61.       }
  62.       if(volume1 instanceof CLineSegment && volume2 instanceof CCVAxisAlignedBox)
  63.       {
  64.          return Intersection.DoIntersectAABLineSegment(CCVAxisAlignedBox(volume2),CLineSegment(volume1));
  65.       }
  66.       if(volume1 instanceof CLineSegment && volume2 instanceof CLineSegment)
  67.       {
  68.          return Intersection.DoIntersectLineSegmentLineSegment(CLineSegment(volume1),CLineSegment(volume2));
  69.       }
  70.    }
  71.    static function DoIntersectCircleCircle(c1, c2)
  72.    {
  73.       var _loc6_ = c1._center.GetDistance(c2._center);
  74.       var _loc4_ = c1._radius + c2._radius - _loc6_;
  75.       if(_loc4_ > 0)
  76.       {
  77.          var _loc1_ = new CCollisionInfo();
  78.          _loc1_.m_delta = c1._center.GetSubtract(c2._center);
  79.          _loc1_.m_normal = _loc1_.m_delta.GetNormal();
  80.          _loc1_.m_interpenetrationDepth = _loc4_;
  81.          var _loc5_ = c1._radius / (c2._radius + c1._radius);
  82.          _loc1_.m_approximateTouchPoint = c1._center.GetAdd(_loc1_.m_delta.GetMultiplyScalar(_loc5_));
  83.          return _loc1_;
  84.       }
  85.       return null;
  86.    }
  87.    static function DoIntersectAABAAB(aabb1, aabb2)
  88.    {
  89.       var _loc5_ = aabb1._left <= aabb2._right && aabb1._right >= aabb2._left;
  90.       var _loc4_ = aabb1._top <= aabb2._bottom && aabb1._bottom >= aabb2._top;
  91.       if(_loc5_ && _loc4_)
  92.       {
  93.          var _loc1_ = new CCollisionInfo();
  94.          _loc1_.m_delta = aabb1._center.GetSubtract(aabb2._center);
  95.          _loc1_.m_normal = _loc1_.m_delta.GetNormal();
  96.          _loc1_.m_interpenetrationDepth = undefined;
  97.          _loc1_.m_approximateTouchPoint = undefined;
  98.          return _loc1_;
  99.       }
  100.       return null;
  101.    }
  102.    static function DoIntersectCircleAAB(circle, aab)
  103.    {
  104.       var _loc3_ = aab._topLeftCorner;
  105.       var _loc2_ = aab._bottomRightCorner;
  106.       var _loc5_ = new Vector2D(_loc2_._x,_loc3_._y);
  107.       var _loc6_ = new Vector2D(_loc3_._x,_loc2_._y);
  108.       var _loc1_ = circle._center;
  109.       var _loc4_ = circle._radius;
  110.       if(_loc1_._x >= _loc3_._x && _loc1_._x <= _loc2_._x && _loc1_._y >= _loc3_._y && _loc1_._y <= _loc2_._y)
  111.       {
  112.          return new CCollisionInfo();
  113.       }
  114.       var _loc10_ = Intersection.IntersectCircleLineSegment(_loc1_,_loc4_,_loc3_,_loc5_);
  115.       var _loc9_ = Intersection.IntersectCircleLineSegment(_loc1_,_loc4_,_loc5_,_loc2_);
  116.       var _loc8_ = Intersection.IntersectCircleLineSegment(_loc1_,_loc4_,_loc2_,_loc6_);
  117.       var _loc7_ = Intersection.IntersectCircleLineSegment(_loc1_,_loc4_,_loc6_,_loc3_);
  118.       if(_loc10_ || _loc9_ || _loc8_ || _loc7_)
  119.       {
  120.          return new CCollisionInfo();
  121.       }
  122.       return null;
  123.    }
  124.    static function DoIntersectLineSegmentLineSegment(segment1, segment2)
  125.    {
  126.       if(segment1._radius > 0 || segment2._radius > 0)
  127.       {
  128.          if(segment1._radius + segment2._radius >= Distance.LineSegmentLineSegment(segment1,segment2))
  129.          {
  130.             return new CCollisionInfo();
  131.          }
  132.          return null;
  133.       }
  134.       return Intersection.IntersectLineSegmentLineSegment(segment1.m_endpoint1,segment1.m_endpoint2,segment2.m_endpoint1,segment2.m_endpoint2);
  135.    }
  136.    static function DoIntersectCircleLineSegment(circle, segment)
  137.    {
  138.       if(segment._radius > 0)
  139.       {
  140.          if(segment._radius >= Distance.CircleLineSegment(circle,segment))
  141.          {
  142.             return new CCollisionInfo();
  143.          }
  144.          return null;
  145.       }
  146.       return Intersection.IntersectCircleLineSegment(circle._center,circle._radius,segment.m_endpoint1,segment.m_endpoint2);
  147.    }
  148.    static function DoIntersectAABLineSegment(aab, segment)
  149.    {
  150.       var _loc3_ = aab._topLeftCorner;
  151.       var _loc2_ = aab._bottomRightCorner;
  152.       var _loc4_ = new Vector2D(_loc2_._x,_loc3_._y);
  153.       var _loc5_ = new Vector2D(_loc3_._x,_loc2_._y);
  154.       if(segment.m_endpoint1._x >= _loc3_._x && segment.m_endpoint1._x <= _loc2_._x && segment.m_endpoint1._y >= _loc3_._y && segment.m_endpoint1._y <= _loc2_._y)
  155.       {
  156.          return new CCollisionInfo();
  157.       }
  158.       if(segment.m_endpoint2._x >= _loc3_._x && segment.m_endpoint2._x <= _loc2_._x && segment.m_endpoint2._y >= _loc3_._y && segment.m_endpoint2._y <= _loc2_._y)
  159.       {
  160.          return new CCollisionInfo();
  161.       }
  162.       if(segment._radius > 0)
  163.       {
  164.          var _loc9_ = new CLineSegment();
  165.          _loc9_.m_endpoint1 = _loc3_;
  166.          _loc9_.m_endpoint2 = _loc4_;
  167.          var _loc8_ = new CLineSegment();
  168.          _loc8_.m_endpoint1 = _loc4_;
  169.          _loc8_.m_endpoint2 = _loc2_;
  170.          var _loc7_ = new CLineSegment();
  171.          _loc7_.m_endpoint1 = _loc2_;
  172.          _loc7_.m_endpoint2 = _loc5_;
  173.          var _loc6_ = new CLineSegment();
  174.          _loc6_.m_endpoint1 = _loc5_;
  175.          _loc6_.m_endpoint2 = _loc3_;
  176.          var _loc13_ = Intersection.DoIntersectLineSegmentLineSegment(segment,_loc9_);
  177.          var _loc12_ = Intersection.DoIntersectLineSegmentLineSegment(segment,_loc8_);
  178.          var _loc11_ = Intersection.DoIntersectLineSegmentLineSegment(segment,_loc7_);
  179.          var _loc10_ = Intersection.DoIntersectLineSegmentLineSegment(segment,_loc6_);
  180.       }
  181.       else
  182.       {
  183.          _loc13_ = Intersection.IntersectLineSegmentLineSegment(segment.m_endpoint1,segment.m_endpoint2,_loc3_,_loc4_);
  184.          _loc12_ = Intersection.IntersectLineSegmentLineSegment(segment.m_endpoint1,segment.m_endpoint2,_loc4_,_loc2_);
  185.          _loc11_ = Intersection.IntersectLineSegmentLineSegment(segment.m_endpoint1,segment.m_endpoint2,_loc2_,_loc5_);
  186.          _loc10_ = Intersection.IntersectLineSegmentLineSegment(segment.m_endpoint1,segment.m_endpoint2,_loc5_,_loc3_);
  187.       }
  188.       if(_loc13_ || _loc12_ || _loc11_ || _loc10_)
  189.       {
  190.          return new CCollisionInfo();
  191.       }
  192.       return null;
  193.    }
  194.    static function IntersectLineSegmentLineSegment(a1, a2, b1, b2)
  195.    {
  196.       var _loc9_ = (b2._x - b1._x) * (a1._y - b1._y) - (b2._y - b1._y) * (a1._x - b1._x);
  197.       var _loc8_ = (a2._x - a1._x) * (a1._y - b1._y) - (a2._y - a1._y) * (a1._x - b1._x);
  198.       var _loc3_ = (b2._y - b1._y) * (a2._x - a1._x) - (b2._x - b1._x) * (a2._y - a1._y);
  199.       if(_loc3_ != 0)
  200.       {
  201.          var _loc7_ = _loc9_ / _loc3_;
  202.          var _loc6_ = _loc8_ / _loc3_;
  203.          if(0 <= _loc7_ && _loc7_ <= 1 && 0 <= _loc6_ && _loc6_ <= 1)
  204.          {
  205.             return new CCollisionInfo();
  206.          }
  207.          return null;
  208.       }
  209.       return null;
  210.    }
  211.    static function IntersectCircleLineSegment(c, r, a1, a2)
  212.    {
  213.       var _loc8_ = (a2._x - a1._x) * (a2._x - a1._x) + (a2._y - a1._y) * (a2._y - a1._y);
  214.       var _loc3_ = 2 * ((a2._x - a1._x) * (a1._x - c._x) + (a2._y - a1._y) * (a1._y - c._y));
  215.       var _loc10_ = c._x * c._x + c._y * c._y + a1._x * a1._x + a1._y * a1._y - 2 * (c._x * a1._x + c._y * a1._y) - r * r;
  216.       var _loc7_ = _loc3_ * _loc3_ - 4 * _loc8_ * _loc10_;
  217.       if(_loc7_ < 0)
  218.       {
  219.          return null;
  220.       }
  221.       if(_loc7_ == 0)
  222.       {
  223.          return null;
  224.       }
  225.       var _loc9_ = Math.sqrt(_loc7_);
  226.       var _loc6_ = (- _loc3_ + _loc9_) / (2 * _loc8_);
  227.       var _loc5_ = (- _loc3_ - _loc9_) / (2 * _loc8_);
  228.       if((_loc6_ < 0 || _loc6_ > 1) && (_loc5_ < 0 || _loc5_ > 1))
  229.       {
  230.          if(_loc6_ < 0 && _loc5_ < 0 || _loc6_ > 1 && _loc5_ > 1)
  231.          {
  232.             return null;
  233.          }
  234.          return new CCollisionInfo();
  235.       }
  236.       return new CCollisionInfo();
  237.    }
  238. }
  239.